/**
 *  Stack with min() function
 *
 */

import java.util.Stack;

public class MinStack<E extends Comparable<E>> {
    
    private Stack<E> dataStack;
    private Stack<E> minStack;
    private int total;

    public MinStack() {
        dataStack = new Stack<E>();
        minStack = new Stack<E>();
    }
    
    public void push(E item) {
        dataStack.push(item);

        E minItem = item;

        if (total > 0) {
            E min = minStack.peek();
            if (min.compareTo(minItem) < 0) {
                minItem = min;
            }
        }

        minStack.push(minItem);

        total++;
    }

    public E pop() {
        if (total == 0) {
            return null;
        }

        minStack.pop();

        E item = dataStack.pop();
        total--;

        return item;
    }

    public E peek() {
        if (total == 0) {
            return null;
        }

        return dataStack.peek();
    }

    public int size() {
        return total;
    }

    public E min() {
        if (total == 0) {
            return null;
        }

        return minStack.peek();
    }

}
